package com.atguigu.crowd.serviceimpl;

import com.atguigu.crowd.entity.Auth;
import com.atguigu.crowd.entity.AuthExample;
import com.atguigu.crowd.service.AuthService;
import com.atguigu.crowd.mapper.AuthMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author yzt
 * @create 2021-09-14 9:12
 */
@Service
public class AuthServiceImpl implements AuthService {

    @Autowired
    private AuthMapper authMapper;

    @Override
    public List<Auth> getAll() {
        List<Auth> authList = authMapper.selectByExample(new AuthExample());
        return authList;
    }

    @Override
    public List<Integer> getAuthByRoleId(Integer roleId) {
        return authMapper.selectAuthByRoleId(roleId);
    }

    @Override
    public void saveAuthList(Map<String, List<Integer>> map) {
        List<Integer> roleIdList = map.get("roleId");
        Integer roleId = roleIdList.get(0);

        authMapper.deleteAuths(roleId);

        List<Integer> authIdList = map.get("authIdArray");
        // 4.判断 authIdList 是否有效
         if(authIdList != null && authIdList.size() > 0) {
             authMapper.insertAuthList(authIdList,roleId);
         }
    }
}
